<?php
namespace App\Http\Controllers\ShopApi\V1\Auth;

use App\Http\Controllers\ShopApiController;
use App\Http\Controllers\Business\Shop\V1\Auth\ShopAuth;
use Log;

class AuthController extends ShopApiController
{
    //获取手机验证码
    public function getCode() {
        $mobile = $this->request->get('mobile');
        if($this->request->has('sub')) {
            $sub = $this->request->get('sub');
        } else {
            $sub = '';
        }
        if($this->request->has('mode')) {
            $mode = $this->request->get('mode');
        } else {
            $mode = 1;
        }
        $result = ShopAuth::getCode($mobile, $mode, $sub);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //登录
    public function login() {
        $mobile = $this->request->get('mobile');
        $code = $this->request->get('code');
        $password = $this->request->get('password');
        if($this->request->has('type')) {
            $type = $this->request->get('type');//短信，获取密码, sms, password
        } else {
            $type = 'sms';
        }
        $versionCode = $this->request->get('versioncode');
        if($versionCode >= 11) {
            $result = ShopAuth::loginByV12Version($mobile,$type, $code, $password);
        } else {
            $result = ShopAuth::login($mobile, $code); //TODO 同一个手机号，多家分店的没有处理
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
    //刷新
    public function refreshToken() {
        $token = $this->request->get('token');
        $result = ShopAuth::refreshToken($token);
        if($result == false) {
            return $this->responseError("认证失败，重新登录");
        }
        $token = $result;
        $data = ['token'=>$token];
        return $this->responseSuccessData($data);
    }
    //登出
    public function logout() {
        $token = $this->request->get('token');
        $result = ShopAuth::apiLogout($token);
        return $this->responseSuccessMessage("已退出登录");
    }
}